﻿// ***************************************************************
//  version:  0.4.0   date: 11/13/2007-2008
//  -------------------------------------------------------------
//  
//  -------------------------------------------------------------
//  Copyright (C) Midapex Software 2003-2007-2008 - All Rights Reserved
// ***************************************************************
// 
// ***************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using Midapex.SmartOrm.Properties;
namespace Midapex.SmartOrm
{
    /// <summary>
    /// 从属性映射的数据列
    /// </summary>
    public class PropertyColumn:Column
    {
        PropertyColumn()
        {

        }
        PropertyInfo prop;
        MethodInfo getter;
        MethodInfo setter;

        /// <summary>
        /// 创建新对象
        /// </summary>
        /// <param name="colAttr">数据列特性</param>
        /// <param name="prop">与之关联的属性信息</param>
        public PropertyColumn(ColumnAttribute colAttr, PropertyInfo prop)
            :base(colAttr)
        {
            if(prop==null)
            {
                throw new ArgumentNullException();
            }

            this.prop = prop;
            getter = prop.GetGetMethod();
            setter = prop.GetSetMethod();

            if(colAttr.Name == null || colAttr.Name == string.Empty)
                name = prop.Name;
            else
                name = colAttr.Name;

            name = name.ToUpper();

            if (colAttr.DbType == DbType.Object)
            {
                //设置数据类型
                this.DbType = BuildDbType(prop.PropertyType);
            }

            if (this.DataType == typeof(object))
            {
                throw new OrmLogicException(Resource.ExDeclareObjectTypeNotAllowed);
            }
        }

        public override Type DataType
        {
            get
            {
                return prop.PropertyType;
            }
        }
       
        public override bool Readable
        {
            get
            {
                return getter != null;
            }
        }

        public override bool Writeable
        {
            get
            {
                return setter != null;
            }
        }

        public override object Read(object target)
        {
            if (!Readable)
                return null;
            return getter.Invoke(target, null);
        }

        public override void Write(object target, object colValue)
        {
            if (!Writeable)
                return;
            setter.Invoke(target, new object[] { colValue });
        }

        public override object Clone()
        {
            PropertyColumn col = new PropertyColumn();
            Copy(col, this);
            col.prop = this.prop;
            col.setter = this.setter;
            col.getter = this.getter;
            return col;
        }
    }
}
